package com.zhanghu.backendZray.strategy.notify;

import com.zhanghu.backendZray.enums.NotifyTypeEnum;
import com.zhanghu.backendZray.pojo.entity.DailyPlan;
import com.zhanghu.backendZray.pojo.entity.NotifySendRecord;
import com.zhanghu.backendZray.pojo.entity.NotifySetting;
import com.zhanghu.backendZray.service.NotifySendRecordService;
import com.zhanghu.backendZray.utils.mail.MailUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author ZhangDong
 * @date 2024/8/30 17:02
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class MailNotifyStrategy implements NotifyStrategy {
    private final NotifySendRecordService mailSendRecordService;

    @Override
    public boolean doNotify(DailyPlan plan, NotifySetting setting) {
        String mail = setting.getMail();
        boolean success = MailUtil.sendEmail(Arrays.asList(mail), plan.getTitle(), plan.getContent());
        log.info("mail notify, mail:{}, title:{}, content:{}, result:{}", mail, plan.getTitle(), plan.getContent(), success);
        NotifySendRecord mailSendRecord = new NotifySendRecord();
        mailSendRecord.setUserId(plan.getUserId());
        mailSendRecord.setType(setting.getType());
        mailSendRecord.setMail(mail);
        mailSendRecord.setTitle(plan.getTitle());
        mailSendRecord.setContent(plan.getContent());
        mailSendRecord.setStatus(success ? (byte) 1 : (byte) 2);
        mailSendRecordService.save(mailSendRecord);
        return success;
    }

    @Override
    public NotifyTypeEnum getNotifyType() {
        return NotifyTypeEnum.MAIL;
    }
}
